// 模拟数据
const mockOutfits = [
  {
    id: '1',
    name: '春季休闲搭配',
    image: '/搭配图片/春季休闲搭配.jpg',
    season: '春季',
    occasion: '休闲',
    occasionName: '休闲',
    clothes: [
      { name: '白色T恤', image: '/images/default-clothes.png' },
      { name: '牛仔裤', image: '/images/default-clothes.png' },
      { name: '运动鞋', image: '/images/default-clothes.png' }
    ],
    createTime: '2024-03-15T08:00:00.000Z'
  },
  {
    id: '2',
    name: '夏季工作搭配',
    image: '/搭配图片/夏季工作搭配.jpg',
    season: '夏季',
    occasion: '工作',
    occasionName: '工作',
    clothes: [
      { name: '衬衫', image: '/images/default-clothes.png' },
      { name: '西裤', image: '/images/default-clothes.png' },
      { name: '皮鞋', image: '/images/default-clothes.png' }
    ],
    createTime: '2024-03-14T08:00:00.000Z'
  },
  {
    id: '3',
    name: '秋季运动搭配',
    image: '/搭配图片/秋季运动搭配.jpg',
    season: '秋季',
    occasion: '运动',
    occasionName: '运动',
    clothes: [
      { name: '运动外套', image: '/images/default-clothes.png' },
      { name: '运动裤', image: '/images/default-clothes.png' },
      { name: '运动鞋', image: '/images/default-clothes.png' }
    ],
    createTime: '2024-03-13T08:00:00.000Z'
  },
  {
    id: '4',
    name: '冬季正式搭配',
    image: '/搭配图片/冬季正式搭配.jpg',
    season: '冬季',
    occasion: '正式',
    occasionName: '正式',
    clothes: [
      { name: '大衣', image: '/images/default-clothes.png' },
      { name: '西装', image: '/images/default-clothes.png' },
      { name: '皮鞋', image: '/images/default-clothes.png' }
    ],
    createTime: '2024-03-12T08:00:00.000Z'
  }
];

// 默认场合列表
const defaultOccasions = [
  { label: '工作', value: '工作' },
  { label: '休闲', value: '休闲' },
  { label: '正式', value: '正式' },
  { label: '运动', value: '运动' }
];

// 初始化本地存储
const initStorage = () => {
  const outfits = wx.getStorageSync('outfits');
  if (!outfits) {
    wx.setStorageSync('outfits', mockOutfits);
  }
  
  const occasions = wx.getStorageSync('occasions');
  if (!occasions) {
    wx.setStorageSync('occasions', defaultOccasions);
  }
};

// 获取场合列表
export function getOccasionList() {
  return new Promise((resolve) => {
    initStorage();
    const occasions = wx.getStorageSync('occasions') || defaultOccasions;
    resolve({ data: occasions });
  });
}

// 添加自定义场合
export function addOccasion(occasion) {
  return new Promise((resolve) => {
    initStorage();
    const occasions = wx.getStorageSync('occasions') || defaultOccasions;
    const newOccasion = {
      label: occasion,
      value: occasion
    };
    
    // 检查是否已存在
    const exists = occasions.some(item => item.value === newOccasion.value);
    if (!exists) {
      occasions.push(newOccasion);
      wx.setStorageSync('occasions', occasions);
    }
    
    resolve({ data: newOccasion });
  });
}

// 获取搭配列表
export function getOutfitList(params) {
  return new Promise((resolve) => {
    initStorage();
    const { pageNum = 1, pageSize = 10, season = '', occasion = '', keyword = '' } = params;
    const outfits = wx.getStorageSync('outfits') || [];
    
    // 过滤搭配列表
    let filteredOutfits = outfits.filter(outfit => {
      const seasonMatch = !season || outfit.season === season;
      const occasionMatch = !occasion || outfit.occasion === occasion;
      const keywordMatch = !keyword || outfit.name.toLowerCase().includes(keyword.toLowerCase());
      return seasonMatch && occasionMatch && keywordMatch;
    });

    // 分页
    const start = (pageNum - 1) * pageSize;
    const end = start + pageSize;
    const pageList = filteredOutfits.slice(start, end);

    resolve({
      data: {
        list: pageList,
        total: filteredOutfits.length
      }
    });
  });
}

// 获取搭配详情
export function getOutfitDetail(id) {
  return new Promise((resolve) => {
    initStorage();
    const outfits = wx.getStorageSync('outfits') || [];
    const outfit = outfits.find(item => item.id === id);
    resolve({ data: outfit });
  });
}

// 创建搭配
export function createOutfit(data) {
  return new Promise((resolve) => {
    initStorage();
    const outfits = wx.getStorageSync('outfits') || [];
    const occasions = wx.getStorageSync('occasions') || defaultOccasions;
    
    // 获取场合名称
    const occasion = occasions.find(item => item.value === data.occasion);
    
    // 处理衣物数据
    const clothes = data.clothes.map(name => ({
      name,
      image: '/images/default-clothes.png' // 默认图片
    }));
    
    const newOutfit = {
      ...data,
      clothes,
      occasionName: occasion ? occasion.label : data.occasion,
      id: Date.now().toString(),
      createTime: new Date().toISOString()
    };
    outfits.unshift(newOutfit);
    wx.setStorageSync('outfits', outfits);
    resolve({ data: newOutfit });
  });
}

// 更新搭配
export function updateOutfit(id, data) {
  return new Promise((resolve) => {
    initStorage();
    const outfits = wx.getStorageSync('outfits') || [];
    const occasions = wx.getStorageSync('occasions') || defaultOccasions;
    const index = outfits.findIndex(item => item.id === id);
    
    if (index > -1) {
      // 获取场合名称
      const occasion = occasions.find(item => item.value === data.occasion);
      
      // 处理衣物数据
      const clothes = data.clothes.map(name => ({
        name,
        image: '/images/default-clothes.png' // 默认图片
      }));
      
      outfits[index] = {
        ...outfits[index],
        ...data,
        clothes,
        occasionName: occasion ? occasion.label : data.occasion,
        updateTime: new Date().toISOString()
      };
      wx.setStorageSync('outfits', outfits);
      resolve({ data: outfits[index] });
    }
  });
}

// 删除搭配
export function deleteOutfit(id) {
  return new Promise((resolve) => {
    initStorage();
    const outfits = wx.getStorageSync('outfits') || [];
    const newOutfits = outfits.filter(item => item.id !== id);
    wx.setStorageSync('outfits', newOutfits);
    resolve({ data: null });
  });
}

// 上传搭配图片
export function uploadOutfitImage(filePath) {
  return new Promise((resolve) => {
    // 由于是前端实现，直接返回本地文件路径
    resolve({
      data: {
        url: filePath
      }
    });
  });
} 